package com.bdqn.controller;

import com.bdqn.LayUI.Layui;
import com.bdqn.Service.ContractApply.ContractApplyService;
import com.bdqn.Service.Enquire.EnquireService;
import com.bdqn.Service.IdMapping.IdMappingService;
import com.bdqn.Service.Orders.OrdersService;
import com.bdqn.Service.QuoteDetail.QuoteDetailSevice;
import com.bdqn.Service.Stock.StockService;
import com.bdqn.Service.Supplier.SupplierService;
import com.bdqn.entity.*;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Controller
@RequestMapping("/contractmanager")
public class ContractApplyController {
    @Autowired
    private ContractApplyService service;
    @Autowired
    private EnquireService enquireService;
    @Autowired
    private StockService stockService;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private QuoteDetailSevice quoteDetailSevice;
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private IdMappingService idMappingService;
    @GetMapping("/jieshilist")
    @ResponseBody
    public Layui<ContractApply> findAllByPage(Integer page, Integer limit,ContractApply apply){
        page = (page-1)*limit;
        List<ContractApply> contractApplies = null;
        apply.setStatus("同意");
        contractApplies = service.findAllByPage(page,limit,apply);
        int count = service.getCount();
        Layui<ContractApply> contractApplyLayui=new Layui<ContractApply>();
        contractApplyLayui.setCode(0);
        contractApplyLayui.setData(contractApplies);
        contractApplyLayui.setCount(count);
        return contractApplyLayui;
    }
    @GetMapping("/jieshilist1")
    @ResponseBody
    public Layui<ContractApply> jieshilist1(Integer page, Integer limit,ContractApply apply){
        page = (page-1)*limit;
        List<ContractApply> contractApplies = null;
        apply.setStatus("未确定");
        contractApplies = service.findAllByPage(page,limit,apply);
        int count = service.getCount();
        Layui<ContractApply> contractApplyLayui=new Layui<ContractApply>();
        contractApplyLayui.setCode(0);
        contractApplyLayui.setData(contractApplies);
        contractApplyLayui.setCount(count);
        return contractApplyLayui;
    }

    @GetMapping("/DaiShen")
    @ResponseBody
    public Layui<ContractApply> DaiShen(Integer page, Integer limit,ContractApply apply){
        page = (page-1)*limit;
        List<ContractApply> contractApplies = null;
        apply.setStatus("待审");
            contractApplies = service.findAllByPage(page,limit,apply);
        int count = service.getCount();
        Layui<ContractApply> contractApplyLayui=new Layui<ContractApply>();
        contractApplyLayui.setCode(0);
        contractApplyLayui.setData(contractApplies);
        contractApplyLayui.setCount(count);
        return contractApplyLayui;
    }
    @GetMapping("/jieshilist2")
    @ResponseBody
    public Layui<ContractApply> findAllByPage2(Integer page, Integer limit,ContractApply apply){
        page = (page-1)*limit;
        List<ContractApply> contractApplies = null;
        apply.setStatus("确定");
        if(apply.getStatus()=="确定"){
            contractApplies = service.findAllByPage(page,limit,apply);
        }
        int count = service.getCount();
        Layui<ContractApply> contractApplyLayui=new Layui<ContractApply>();
        contractApplyLayui.setCode(0);
        contractApplyLayui.setData(contractApplies);
        contractApplyLayui.setCount(count);
        return contractApplyLayui;
    }

    @GetMapping("/jieshilist3")
    @ResponseBody
    public Layui<ContractApply> findAllByPage3(Integer page, Integer limit,ContractApply apply){
        page = (page-1)*limit;
        List<ContractApply> contractApplies1 = service.findAllByPage2(page, limit, apply);
        int count = service.getCount();
        Layui<ContractApply> contractApplyLayui=new Layui<ContractApply>();
        contractApplyLayui.setCode(0);
        contractApplyLayui.setData(contractApplies1);
        contractApplyLayui.setCount(count);
        return contractApplyLayui;
    }

    @RequestMapping("/Apply_Xq")
    @ResponseBody
    public ContractApply applyXq(ContractApply contractApply){
        ContractApply byId = service.findById(contractApply);
        List<Enquire> mateSupByEnq = enquireService.findMateSupByEnq(byId.getEnquire().getId());
        List<Material> materials=new ArrayList<Material>();
        for (Enquire enquire : mateSupByEnq) {
            for (Material material : enquire.getMaterials()) {
                materials.add(material);
            }

        }
        Stock stock=new Stock();
        stock.setAppid(byId.getEnquire().getId());
        Stock sto = stockService.findById(stock);
        byId.getEnquire().setStock(sto);
        byId.setMaterials(materials);
        Orders orders=new Orders();
        orders.setAddid(byId.getEnquire().getId());
        List<Orders> byId1 = ordersService.findById1(orders);
        byId.setOrders(byId1);
        Calendar calendar=Calendar.getInstance();
        byId.setContAppNum("1100"+calendar.get(Calendar.YEAR)+calendar.get(Calendar.MONTH)+calendar.get(Calendar.DATE)+calendar.get(Calendar.HOUR_OF_DAY)+calendar.get(Calendar.MINUTE)+calendar.get(Calendar.SECOND));
        return byId;
    }
    @RequestMapping("/Apply_XqX")
    @ResponseBody
    public Orders applyXqX(Orders orders){
        orders.setId(95);
        Orders byId1 = ordersService.findById(orders);
        return byId1;
    }

    @RequestMapping("/SelQuoByEnquireId")
    @ResponseBody
    public ContractApply SelByOrderId(@Param("Num") String Num){
        ContractApply contractApply=new ContractApply();
        QuoteDetail quoteDetail=new QuoteDetail();
        Stock stock=stockService.SelectByNum(Num);
        quoteDetail.setEnquireId(stock.getStockNum());
        List<QuoteDetail> quoteDetails = quoteDetailSevice.SelByEuiId(quoteDetail);
        contractApply.setStatQuote(quoteDetails.size()+"");
        Double MAXB=0.0;
        Double MIXB=100000000.0;
        Double MUN=0.0;
        for (QuoteDetail detail : quoteDetails) {
            if(detail.getTotalPrice()>MAXB){
                MAXB=detail.getTotalPrice();
            }
            if(detail.getTotalPrice()<MIXB){
                MIXB=detail.getTotalPrice();
            }
            MUN+= detail.getTotalPrice();
        }
        contractApply.setTopQuote(MAXB+"");
        contractApply.setLowQuote(MIXB+"");
        contractApply.setAllSumPrice(MUN);
        return contractApply;
    }

    @RequestMapping("/SelByEnquireId")
    @ResponseBody
    public Enquire SelByEnquireId(@Param("id") int id){
        Enquire enquire=new Enquire();
        enquire.setId(id);
        Enquire byNum = enquireService.findByNum(enquire);
        return byNum;
    }

    @RequestMapping("/SelOrdersByEnqId")
    @ResponseBody
    public List<Orders> SelOrdersByEnqId(@Param("id") int id){
        List<Orders> orders=ordersService.SelByEnqId(id);
        return orders;
    }

    @RequestMapping("/SelQuoByOrderId")
    @ResponseBody
    public List<QuoteDetail> SelQuoByOrderId(@Param("id") int id,@Param("Num")String Num){
        Stock stock=stockService.SelectByNum(Num);
        QuoteDetail quoteDetail=new QuoteDetail();
        quoteDetail.setOrderId(id);
        quoteDetail.setEnquireId(stock.getStockNum());
        List<QuoteDetail> quoteDetails=quoteDetailSevice.SelByOrderId(quoteDetail);
        return quoteDetails;
    }

    @RequestMapping("/SelGysById")
    @ResponseBody
    public Supplier SelGysById(@Param("id") int id){
        Supplier supplier = supplierService.selectSupById(id);
        return supplier;
    }

    @RequestMapping("/SelSupByQuo")
    @ResponseBody
    public List<Supplier> SelSupByQuo(@Param("id") int id){
        Supplier supplier=new Supplier();
        supplier.setId(id);
        List<Supplier> suppliers = supplierService.SelSupByQuo(supplier);
        return suppliers;
    }

    @RequestMapping("/SelOrdById")
    @ResponseBody
    public Orders SelOrdById(@Param("id") int id){
        Orders orders=new Orders();
        orders.setId(id);
        Orders byId = ordersService.findById(orders);
        return byId;
    }

    @RequestMapping("/UpdateIdMapping")
    @ResponseBody
    public boolean UpdateIdMapping(IdMapping idMapping){
        boolean b=idMappingService.Update(idMapping);
        return b;
    }

    @RequestMapping("/QueDing")
    @ResponseBody
    public boolean QueDing(@Param("enquireName") String enquireName,@Param("enquireId") int enquireId,@Param("statQuote")String statQuote,
                           @Param("topQuote")String topQuote,@Param("lowQuote")String lowQuote,@Param("allSumPrice")String allSumPrice,@Param("remark")String remark,@Param("code")String code){
        ContractApply contractApply=new ContractApply();
        contractApply.setStatQuote(statQuote);
        contractApply.setTopQuote(topQuote);
        contractApply.setLowQuote(lowQuote);
        if(allSumPrice!=null){
            contractApply.setAllSumPrice(Double.parseDouble(allSumPrice));
        }
        contractApply.setRemark(remark);
        contractApply.setCode(code);
        IdMapping idMapping=new IdMapping();
        idMapping.setEnquireId(enquireId);
        Calendar calendar=Calendar.getInstance();
        contractApply.setContAppNum("1010"+calendar.get(Calendar.YEAR)+calendar.get(Calendar.MONTH)+calendar.get(Calendar.DATE)+calendar.get(Calendar.HOUR_OF_DAY)+calendar.get(Calendar.MINUTE)+calendar.get(Calendar.SECOND));
        if(contractApply.getCode()!=null){
            Enquire enquire=new Enquire();
            enquire.setStatus1("已申请");
            enquire.setEnquireName(enquireName);
            boolean bool=enquireService.UpdateStatus1(enquire);
            boolean b=service.insert(contractApply);
        }
        int ApplyId=service.SelMaxId();
        idMapping.setContAppId(ApplyId);
        idMappingService.UpdateId(idMapping);
        return true;
    }
    @RequestMapping("/updateStatus")
    @ResponseBody
    public boolean updateStatus(ContractApply apply){
        apply.setStatus("确定");
        return service.updateStatus(apply);
    }
    @RequestMapping("/updateStatus2")
    @ResponseBody
    public boolean updateStatus2(ContractApply apply){
        apply.setStatus("待审");
        return service.updateStatus(apply);
    }
    @RequestMapping("/updateStatus3")
    @ResponseBody
    public boolean updateStatus3(ContractApply apply){
        apply.setStatus("未确定");
        return service.updateStatus(apply);
    }
    @RequestMapping("/UpdateByIdPr")
    @ResponseBody
    public boolean UpdateByIdPr(ContractApply apply){
        Subject subject= SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getSession().getAttribute("employee");//员工
        apply.setLeader(employee.getEmpName());
        return service.updateStatus(apply);
    }

    @RequestMapping("/findContAppById")
    @ResponseBody
    public ContractApply findContAppById(ContractApply apply){
        return service.findConAppById(apply);
    }

    @RequestMapping("/findContAppId")
    @ResponseBody
    public List<QuoteDetail> findContAppId(QuoteDetail quoteDetail){
        List<QuoteDetail> contAppId = quoteDetailSevice.findContAppId(quoteDetail);
        return contAppId;
    }

    @RequestMapping("/Del")
    @ResponseBody
    public boolean Del(@Param("id") int id){
        boolean b=idMappingService.UpdateByAppId(id);
        boolean c=service.Del(id);
        return true;
    }

}
